package cz.khfotbal.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cz.khfotbal.dao.SeasonCompetionTeamMatchDao;
import cz.khfotbal.domain.Match;
import cz.khfotbal.domain.SeasonCompetionTeam;
import cz.khfotbal.domain.SeasonCompetionTeamMatch;

@Repository
@Transactional
public class SeasonCompetionTeamMatchDaoImpl implements
		SeasonCompetionTeamMatchDao {

	@Autowired
	private SessionFactory sessionFactory;

	private Session session;

//	public List<SeasonCompetionTeamMatch> findBySeasonCompetionTeam(
//			SeasonCompetionTeam sct) {
//		session = sessionFactory.getCurrentSession();
//		List<SeasonCompetionTeamMatch> list = session.createQuery(
//				"from SeasonCompetionTeamMatch sctm where sctm.seasonCompetionTeam.id="
//						+ sct.getId() + " and sctm.homeTeam=1").list();
//		return list;
//
//	}
	
	public List<SeasonCompetionTeamMatch> findBySeasonCompetionTeam(
			SeasonCompetionTeam sct) {
		session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(
				"from SeasonCompetionTeamMatch sctm where sctm.seasonCompetionTeam.id="
						+ sct.getId() + " and sctm.homeTeam=1");
		query.setCacheable(true);
		List<SeasonCompetionTeamMatch> list = query.list();
		return list;

	}

	public List<SeasonCompetionTeamMatch> findBySeasonCompetionTeamAll(
			SeasonCompetionTeam sct) {
		session = sessionFactory.getCurrentSession();
		List<SeasonCompetionTeamMatch> list = session.createQuery(
				"from SeasonCompetionTeamMatch sctm where sctm.seasonCompetionTeam.id="
						+ sct.getId()).list();
		return list;

	}

//	public List<SeasonCompetionTeamMatch> findByMatch(Match match) {
//		session = sessionFactory.getCurrentSession();
//		List<SeasonCompetionTeamMatch> list = session.createQuery(
//				"from SeasonCompetionTeamMatch sctm where sctm.match.id="
//						+ match.getId()).list();
//		return list;
//	}
	
	public List<SeasonCompetionTeamMatch> findByMatch(Match match) {
		session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(
				"from SeasonCompetionTeamMatch sctm where sctm.match.id="
						+ match.getId());
		query.setCacheable(true);
		List<SeasonCompetionTeamMatch> list = query.list();
		return list;
	}

	public void save(SeasonCompetionTeamMatch SCTM) {
		session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(SCTM);
	}

}
